﻿// <copyright file="MainPage.xaml.cs" company="Marcin Wosik">
// Copyright (c) 2010 All Right Reserved
// </copyright>
// <author>Marcin Wosik</author>
// <email>mawo11@gmail.com</email>
// <date>2010-02-25</date>
// <summary></summary>
namespace GameOfLife
{
    using System;
    using System.Globalization;
    using System.Threading;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media.Imaging;
    using GameOfLife.Assets.Resources;
    using GameOfLife.Controls;
    using GameOfLife.GameData;
    using GameOfLife.Helpers;
    using GameOfLife.Interfaces;

    public partial class MainPage : UserControl
    {
        #region Fields

        internal GameSettings Data;
        internal SimulationEngine SimulationEngine;

        #endregion Fields

        #region Constructors

        public MainPage()
        {
            InitializeComponent();

            if (this.Data == null)
            {
                this.Data = GameSettings.Default();
            }
            this.Toolbox.Data = this.Data;
            this.SimulationEngine = new SimulationEngine();
            this.SimulationEngine.Data = this.Data;
            this.Board.Data = this.Data;
            this.Board.SubscribeToSimulationEngine(this.SimulationEngine);

            this.Toolbox.Command += ToolBoxUpdateData;
            this.Player.Command += PlayerCommandHandler;
            this.SimulationEngine.EndGame += SimulationEngineEndGame;
            UpdateSelectedFlag();
            UpdatePlayer();
            
        }

        #endregion Constructors

        #region Methods

        private static void ChangeLang(string lang)
        {
            LocalizationString localizationString = Application.Current.Resources["LocalizationString"] as LocalizationString;
            if (localizationString != null)
            {
                CultureInfo newCulture = new CultureInfo(lang);
                Thread.CurrentThread.CurrentCulture = newCulture;
                Thread.CurrentThread.CurrentUICulture = newCulture;
                ApplicationStrings.Culture = newCulture;
                localizationString.Strings = new GameOfLife.Assets.Resources.ApplicationStrings();
            }
        }

        private void ChangeToLangEn(object sender, RoutedEventArgs e)
        {
            ChangeLang("en");
            UpdateSelectedFlag();
        }

        private void ChangeToLangPl(object sender, RoutedEventArgs e)
        {
            ChangeLang("pl-PL");
            UpdateSelectedFlag();
        }

        private void UpdateSelectedFlag()
        {
            SelectedFlag.Source = new BitmapImage(new Uri("Assets/Images/Flag." + Thread.CurrentThread.CurrentUICulture.Name + ".png", UriKind.Relative));
        }

        private void PlayerCommandHandler(object sender, CommandEventArgs<PlayerCommand> e)
        {
            switch (e.Command)
            {
                case PlayerCommand.Start:

                    this.SimulationEngine.Start();
                    this.Toolbox.EditMode = false;
                    break;

                case PlayerCommand.Pause:
                    this.SimulationEngine.Stop();
                    break;

                case PlayerCommand.Stop:
                    this.SimulationEngine.Stop();
                    this.Toolbox.EditMode = true;
                    break;

                case PlayerCommand.NextStep:
                    this.SimulationEngine.Step();
                    break;
            }
        }

        private void SimulationEngineEndGame(object sender, System.EventArgs e)
        {
            this.Toolbox.EditMode = true;
            this.Player.Stop();
        }

        private void StackPanelSizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.Board.Width = this.ActualWidth;
            this.Board.Height = this.ActualHeight - 100;
        }

        private void ToolBoxUpdateData(object sender, CommandEventArgs<ToolboxCommand> e)
        {
            Toolbox tolboxItem = sender as Toolbox;
            if (tolboxItem == null)
            {
                return;
            }

            // Data = tolboxItem.Data;

            switch (e.Command)
            {
                case ToolboxCommand.Draw:
                    MakeBoard();
                    break;

                case ToolboxCommand.Random:
                    if (Data.Elements == null || Data.Elements.Count == 0)
                    {
                        MakeBoard();
                    }
                    else
                    {
                        if (this.SimulationEngine.IsRunning)
                        {
                            this.SimulationEngine.Stop();
                        }
                    }
                    this.Board.Random();
                    break;

                case ToolboxCommand.UpdateTimer:
                    if (this.SimulationEngine.IsRunning)
                    {
                        this.SimulationEngine.ChangeTimer();
                    }
                    break;

                case ToolboxCommand.UpdateColoringMode:
                    for (int i = 0; i < this.Data.Elements.Count; ++i)
                    {
                        IElement item = this.Data.Elements[i];
                        item.ColoringMode = this.Data.ColoringMode;
                        item.UpdateColor();
                    }
                    break;

                case ToolboxCommand.UpdateConnectionType:
                    for (int i = 0; i < this.Data.Elements.Count; ++i)
                    {
                        IElement item = this.Data.Elements[i];
                        item.MakeConnection(this.Data);
                    }
                    break;
            }
        }

        private void MakeBoard()
        {
            if (this.SimulationEngine.IsRunning)
            {
                this.SimulationEngine.Stop();
            }
            this.Board.Data = this.Data;
            this.Board.InitializeBoard();
            this.SimulationEngine.Data = this.Data;
            UpdatePlayer();
        }

        private void UpdatePlayer()
        {
            this.Player.IsEnabled = !(this.Data.Elements == null || this.Data.Elements.Count == 0);
        }

        #endregion Methods
    }
}